package com.example.service.impl;

import com.example.dao.UserDao;
import com.example.entity.result.Result;
import com.example.entity.result.ResultBean;
import com.example.entity.vo.User;
import com.example.exception.OperatorException;
import com.example.service.UserService;
import com.example.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    //查询
    @Override
    public Result query(String key, String value) {
        DataUtils.paramCheckEmpty(key, value);
        switch (key) {
            case "all":
                return DataUtils.getResult(Result.SUCCESS, "userAll",
                        userDao.selectAll());
            case "byId":
                return DataUtils.getResult(Result.SUCCESS, "user",
                        userDao.selectById(DataUtils.paramToInteger(value)));
            case "byName":
                return DataUtils.getResult(Result.SUCCESS, "user",
                        userDao.selectByUserName(value));
            case "byType":
                return DataUtils.getResult(Result.SUCCESS, "users",
                        userDao.selectByType(DataUtils.paramToInteger(value)));
            case "byPhone":
                return getPhone(value);
            default:
                return DataUtils.getResult(Result.PARAMETER_INVALID);
        }
    }

    private Result getPhone(String value) {
        User user = userDao.selectById(DataUtils.paramToInteger(value));
        return DataUtils.getResult(Result.SUCCESS, "int", user.getUPhone());
    }

    //登录
    @Override
    public Result login(String username, String password) {
        User user = userDao.selectByUserName(username);
        HashMap<String, User> map = new HashMap<>();
        if (user == null || !password.equals(user.getUPassword())) {
            return DataUtils.getResult(Result.ERROR, "用户名或密码错误");
        } else {
            map.put("data", user);
            return DataUtils.getResult(Result.SUCCESS, "登录成功", map);
        }
    }

    //注册
    @Override
    public Result register(User user) {
        float com = DataUtils.dataCompare(user, new User(), User.class);
        if (user == null || com == 1.0) {
            return DataUtils.getResult(Result.PARAMETER_EMPTY, "参数为空！");
        }
        if (com > 0.3 && com < 1.0) {
            return DataUtils.getResult(Result.PARAMETER_MISSING, "缺少注册信息");
        }
        if (userDao.selectByUserName(user.getUname()) != null) {
            return DataUtils.getResult(Result.PARAMETER_INVALID, "用户名重复");
        }
        int add = userDao.add(user);
        return (add <= 0) ? DataUtils.getResult(Result.ERROR, "注册失败")
                : DataUtils.getResult(Result.SUCCESS, "注册成功");
    }

    //个人信息更新
    @Override
    public Result updateUser(User user) {
        if (DataUtils.dataCompare(userDao.selectById(user.getUid()), user, User.class) == 1) {
            return DataUtils.getResult(Result.SUCCESS, "数据未发生改变");
        }
        int update = userDao.update(user);
        return (update <= 0) ? DataUtils.getResult(Result.ERROR, "更新失败")
                : DataUtils.getResult(Result.SUCCESS, "更新成功");
    }

    //删除
    @Override
    public Result deleteUser(int id) {
        if (userDao.selectById(id) == null) {
            throw new OperatorException("该用户不存在", Result.PARAMETER_INVALID);
        }
        int delete = userDao.delete(id);
        return (delete > 0) ? DataUtils.getResult(Result.SUCCESS)
                : DataUtils.getResult(Result.ERROR, "删除失败");
    }

    //充值消费
    @Override
    public ResultBean pay(User user, String key, float v) {
        float uPrice = user.getUPrice();
        if (v > uPrice) {
            return DataUtils.getResultBean(Result.PARAMETER_INVALID, "", "余额不足");
        }
        if (v == 0) {
            return DataUtils.getResultBean(Result.PARAMETER_EMPTY, "", "数据为空");
        }
        if (key.equals("recharge")) {
            //充值
            uPrice += v;
            return getResultBean(user, uPrice);
        } else if (key.equals("payment")) {
            //消费
            uPrice -= v;
            return getResultBean(user, uPrice);
        } else {
            return DataUtils.getResultBean(Result.PARAMETER_INVALID, "", "参数未知");
        }
    }

    private ResultBean getResultBean(User user, float uPrice) {
        int pay = userDao.pay(user.getUid(), uPrice);
        if (pay > 0) {
            User user1 = userDao.selectById(user.getUid());
            return DataUtils.getResultBean(Result.SUCCESS, "", "余额更新成功",user1);
        } else {
            return DataUtils.getResultBean(Result.ERROR, "", "余额更新失败");
        }
    }


}
